package com.sdry.web.controller.lqy;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.StationStatus;
import com.sdry.model.jyy.Shedudetail;
import com.sdry.model.lqy.AllWorkstation;
import com.sdry.model.lqy.Workstation;
import com.sdry.model.lqy.WorkstationDetail;
import com.sdry.service.lqy.WeeklyProductionReportService;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.WorkstationUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: WeeklyProductionReportController 
 * @Description:报表Controller
 * @Author lqy
 * @Date 2018年12月25日 10:05 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/report")
public class WeeklyProductionReportController {

//	private static int Speed;
	@Resource
	private WeeklyProductionReportService weeklyproductionreport;
	
	
	/**
	 * 查询综合报表
	 * @param time 查询时间    
	 * @param teamName 查询班组   
	 * @param response
	 * @throws Exception
	 */
	
	
	
	
	@RequestMapping(value="/comprehensiveday")
	public  void comprehensiveday(String time) throws Exception{	
		weeklyproductionreport.updatacomprehening(time);
	}
      
	/**
	 * 新综合日报表
	 * 
	 * @param time     查询时间
	 * @param teamName 查询班组
	 * @param response
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/change")
	public void Workchange(String time, HttpServletResponse response) throws Exception {
		if (null == time || time.equals("")) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");// 设置日期格式
			time = df.format(new Date());
		}
		Map<String, Object> map = new HashMap();
		Integer years = Integer.parseInt(time.toString().substring(0, 4));
		Integer months = Integer.parseInt(time.toString().substring(5, 7));// 传的月份
		Calendar calendar = Calendar.getInstance();

		int days = calendar.get(Calendar.DAY_OF_MONTH);
		int smonth = calendar.get(Calendar.MONTH) + 1;// 当前月份

		List<AllWorkstation> work=new ArrayList<>();
		if (months != smonth) {// 查询其他月
			//System.out.println("查其他月");
			List list = new ArrayList();
			Calendar aCalendar = Calendar.getInstance();
			aCalendar.clear();
			aCalendar.set(Calendar.YEAR, years);// 设置年
			aCalendar.set(Calendar.MONTH, months);// 设置月
			int year = aCalendar.get(Calendar.YEAR);// 获取年份
			int month = aCalendar.get(Calendar.MONTH);// 获取月份
			//System.out.println(month + "月");
			int day = DateUtil.getDays(time);// 获取天数
			//System.out.println(day + "天数");
			for (int i = 1; i <= day; i++) {
				String aDate = null;
				if (month < 10 && i < 10) {
					aDate = String.valueOf(year) + "-0" + month + "-0" + i;
				}
				if (month < 10 && i >= 10) {
					aDate = String.valueOf(year) + "-0" + month + "-" + i;
				}
				if (month >= 10 && i < 10) {
					aDate = String.valueOf(year) + "-" + month + "-0" + i;
				}
				if (month >= 10 && i >= 10) {
					aDate = String.valueOf(year) + "-" + month + "-" + i;
				}
				list.add(aDate);
			}
			map.put("begintime", list.get(0));
			map.put("endtime", list.get(day - 1));
			work=weeklyproductionreport.selectcomprehen(map);

		} else {// 查询本月
			//System.out.println("查本月");
			List list = new ArrayList();
			Calendar aCalendar = Calendar.getInstance();
			aCalendar.clear();
			aCalendar.set(Calendar.YEAR, years);// 设置年
			aCalendar.set(Calendar.MONTH, months);// 设置月
			int year = aCalendar.get(Calendar.YEAR);// 获取年份
			int month = aCalendar.get(Calendar.MONTH);// 获取月份
			aCalendar.set(Calendar.DAY_OF_MONTH, 0);// 设置天数
			int day = calendar.get(Calendar.DAY_OF_MONTH);// 当前天数
			//System.out.println(day + "天数");
			for (int i = 1; i <= day; i++) {
				String aDate = null;
				if (month < 10 && i < 10) {
					aDate = String.valueOf(year) + "-0" + month + "-0" + i;
				}
				if (month < 10 && i >= 10) {
					aDate = String.valueOf(year) + "-0" + month + "-" + i;
				}
				if (month >= 10 && i < 10) {
					aDate = String.valueOf(year) + "-" + month + "-0" + i;
				}
				if (month >= 10 && i >= 10) {
					aDate = String.valueOf(year) + "-" + month + "-" + i;
				}
				list.add(aDate);
			}
			map.put("begintime", list.get(0));
			map.put("endtime", list.get(day - 1));
			work=weeklyproductionreport.selectcomprehen(map);
		}
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(work, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}

	
	
	
}
